<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="合同编号" prop="contractNumber">
        <el-input
          v-model="queryParams.contractNumber"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <el-input
          v-model="queryParams.contractName"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签约甲方" prop="signerPartyA">
        <el-input
          v-model="queryParams.signerPartyA"
          placeholder="请输入签约甲方"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签约乙方" prop="signerPartyB">
        <el-input
          v-model="queryParams.signerPartyB"
          placeholder="请输入签约乙方"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输总量" prop="totalTransportVolume">
        <el-input
          v-model="queryParams.totalTransportVolume"
          placeholder="请输入运输总量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输单价" prop="transportUnitPrice">
        <el-input
          v-model="queryParams.transportUnitPrice"
          placeholder="请输入运输单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="合同状态" prop="contractStatus">
        <el-select v-model="queryParams.contractStatus" placeholder="请选择合同状态" clearable>
          <el-option
            v-for="dict in dict.type.contracts_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:contracts:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:contracts:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:contracts:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:contracts:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同id" align="center" prop="contractId" />
      <el-table-column label="合同编号" align="center" prop="contractNumber" />
      <el-table-column label="合同名称" align="center" prop="contractName" />
      <el-table-column label="签约甲方" align="center" prop="signerPartyA" />
      <el-table-column label="签约乙方" align="center" prop="signerPartyB" />
      <el-table-column label="合同状态" align="center" prop="contractStatus" width="180">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.contracts_status" :value="scope.row.contractStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="运输总量" align="center" prop="totalTransportVolume" />
      <el-table-column label="运输单价" align="center" prop="transportUnitPrice" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="230">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:contracts:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:contracts:remove']"
          >删除</el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['system:contracts:carrierSign', 'system:contracts:shipperSign','system:plans:add']">
                <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="carrierSign" icon="el-icon-key"
                    v-hasPermi="['system:contracts:carrierSign']">承运人签字</el-dropdown-item>
                  <el-dropdown-item command="shipperSign" icon="el-icon-circle-check"
                    v-hasPermi="['system:contracts:shipperSign']">托运人签字</el-dropdown-item>
                  <el-dropdown-item command="createPlan" icon="el-icon-circle-check"
                    v-hasPermi="['system:plans:add']">创建计划</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同编号" prop="contractNumber">
          <el-input v-model="form.contractNumber" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合同名称" />
        </el-form-item>
        <el-form-item label="签约甲方" prop="signerPartyA">
          <el-input v-model="form.signerPartyA" placeholder="请输入签约甲方" />
        </el-form-item>
        <el-form-item label="签约乙方" prop="signerPartyB">
          <el-input v-model="form.signerPartyB" placeholder="请输入签约乙方" />
        </el-form-item>
        <el-form-item label="运输总量" prop="totalTransportVolume">
          <el-input v-model="form.totalTransportVolume" placeholder="请输入运输总量" />
        </el-form-item>
        <el-form-item label="运输单价" prop="transportUnitPrice">
          <el-input v-model="form.transportUnitPrice" placeholder="请输入运输单价" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改运输计划对话框 -->
    <el-dialog :title="title" :visible.sync="planOpen" width="500px" append-to-body>
      <el-form ref="planForm" :model="planForm" :rules="planRules" label-width="80px">
        <el-form-item label="计划编号" prop="planNumber">
          <el-input v-model="planForm.planNumber" placeholder="请输入计划编号" />
        </el-form-item>
        <el-form-item label="计划名称" prop="planName">
          <el-input v-model="planForm.planName" placeholder="请输入计划名称" />
        </el-form-item>
        <el-form-item label="计划运输量" prop="plannedTransportVolume">
          <el-input v-model="planForm.plannedTransportVolume" placeholder="请输入计划运输量" />
        </el-form-item>
        <el-form-item label="托运人" prop="shipper">
          <el-input v-model="planForm.shipper" placeholder="请输入托运人" />
        </el-form-item>
        <el-form-item label="发货地址" prop="shipperAddress">
          <el-input v-model="planForm.shipperAddress" placeholder="请输入发货地址" />
        </el-form-item>
        <el-form-item label="收货人" prop="consignee">
          <el-input v-model="planForm.consignee" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="收货地址" prop="consigneeAddress">
          <el-input v-model="planForm.consigneeAddress" placeholder="请输入收货地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPlanForm">确 定</el-button>
        <el-button @click="cancelPlan">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContracts, getContracts, delContracts, addContracts, updateContracts, shipperSign, carrierSign } from "@/api/system/contracts";
import { listPlans, getPlans, delPlans, addPlans, updatePlans } from "@/api/system/plans";
export default {
  name: "Contracts",
  dicts: ['contracts_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      contractsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      planOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNumber: null,
        contractName: null,
        signerPartyA: null,
        signerPartyB: null,
        contractStatus: null,
        totalTransportVolume: null,
        transportUnitPrice: null,
        createBy: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单参数
      planForm: {},
      // 表单校验
      rules: {
        contractNumber: [
          { required: true, message: "合同编号不能为空", trigger: "blur" }
        ],
        contractName: [
          { required: true, message: "合同名称不能为空", trigger: "blur" }
        ],
        signerPartyA: [
          { required: true, message: "签约甲方不能为空", trigger: "blur" }
        ],
        signerPartyB: [
          { required: true, message: "签约乙方不能为空", trigger: "blur" }
        ],
        contractStatus: [
          { required: true, message: "合同状态不能为空", trigger: "change" }
        ],
        totalTransportVolume: [
          { required: true, message: "运输总量不能为空", trigger: "blur" }
        ],
        transportUnitPrice: [
          { required: true, message: "运输单价不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
      },
      // 添加计划表单校验
      planRules: {
        planNumber: [
          { required: true, message: "计划编号不能为空", trigger: "blur" }
        ],
        planName: [
          { required: true, message: "计划名称不能为空", trigger: "blur" }
        ],
        plannedTransportVolume: [
          { required: true, message: "计划运输量不能为空", trigger: "blur" }
        ],
        shipper: [
          { required: true, message: "托运人不能为空", trigger: "blur" }
        ],
        shipperAddress: [
          { required: true, message: "发货地址不能为空", trigger: "blur" }
        ],
        consignee: [
          { required: true, message: "收货人不能为空", trigger: "blur" }
        ],
        consigneeAddress: [
          { required: true, message: "收货地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listContracts(this.queryParams).then(response => {
        this.contractsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 计划取消按钮
    cancelPlan() {
      this.planOpen = false;
      this.resetPlan();
    },
    // 表单重置
    reset() {
      this.form = {
        contractId: null,
        contractNumber: null,
        contractName: null,
        signerPartyA: null,
        signerPartyB: null,
        contractStatus: null,
        totalTransportVolume: null,
        transportUnitPrice: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    // 表单重置
    resetPlan() {
      this.planForm = {
        planId: null,
        contractId: null,
        planNumber: null,
        planName: null,
        plannedTransportVolume: null,
        shipper: null,
        shipperAddress: null,
        consignee: null,
        consigneeAddress: null,
        planStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("planForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contractId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contractId = row.contractId || this.ids
      getContracts(contractId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合同";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.contractId != null) {
            updateContracts(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContracts(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 计划提交按钮 */
    submitPlanForm() {
      this.$refs["planForm"].validate(valid => {
        if (valid) {
          console.log(this.planForm);
          addPlans(this.planForm).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.planOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contractIds = row.contractId || this.ids;
      this.$modal.confirm('是否确认删除合同编号为"' + contractIds + '"的数据项？').then(function() {
        return delContracts(contractIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 承运人签字 */
    carrierSign(row) {
      const contractIds = row.contractId || this.ids;
      this.$modal.confirm('是否确认签约合同编号为"' + contractIds + '"的数据项？').then(function() {
        return carrierSign(contractIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("签约成功");
      }).catch(() => {});
    },
    /** 托运人签字 */
    shipperSign(row) {
      const contractIds = row.contractId || this.ids;
      this.$modal.confirm('是否确认签约合同编号为"' + contractIds + '"的数据项？').then(function() {
        return shipperSign(contractIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("签约成功");
      }).catch(() => {});
    },
    /** 创建运输计划 */
    createPlan(row) {
      this.reset();
      this.planForm.contractId = row.contractId;
      console.log(row.id);
      console.log(this.planForm);
      this.planOpen = true;
      this.title = "创建运输计划";
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "shipperSign":
          this.shipperSign(row);
          break;
        case "carrierSign":
          this.carrierSign(row);
          break;
        case "createPlan":
          this.createPlan(row);
        default:
          break;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/contracts/export', {
        ...this.queryParams
      }, `contracts_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
